package com.lx.commonlib.utils.ui;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * 类说明：更据资源id获取资源的工具类,使用前在
 * Created by liuxiong on 2019/3/23.
 */

public class ResourcesUtil {

    public static Context mContext;

    /**
    * 方法说明:
    * created by liuxiong on 2019/3/23 16:16
    */
    public static void init(Context context){
        mContext=context.getApplicationContext();
    }

    public static Context getContext() {
        if(mContext==null){
            throw new NullPointerException("mContext 为空，请先初始化ResourcesUtil");
        }else{
            return mContext;
        }
    }

    public static Resources getResources() {
        return getContext().getResources();
    }

    /**
    * 方法说明:  获取资源图片
    * created by liuxiong on 2020/1/13 15:01
    */
    public static Drawable getDrawable(int resId)
    {
        return getDrawable( resId,null);
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressWarnings("deprecation")
    public static Drawable getDrawable(int resId, Resources.Theme theme)
    {
        Resources resources=getContext().getResources();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            return resources.getDrawable(resId, theme);
        else
            return resources.getDrawable(resId);
    }

    /**
     * 方法说明:  获取颜色
     * created by liuxiong on 2020/1/13 15:07
     */
    public static int getColor(int resId)
    {
        return getColor(resId,null);
    }

    @TargetApi(Build.VERSION_CODES.M)
    @SuppressWarnings("deprecation")
    public static int getColor( int resId, Resources.Theme theme)
    {

        Resources resources=getContext().getResources();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            return resources.getColor(resId, theme);
        else
            return resources.getColor(resId);
    }


    /**
     * 方法说明:  inflate布局
     * created by liuxiong on 2020/1/13 15:01
     */
    public static View inflate(int resId, ViewGroup parent, boolean isAdd) {
        return LayoutInflater.from(getContext()).inflate(resId, parent,isAdd);
    }
    public static View inflate(int resId) {
        return LayoutInflater.from(getContext()).inflate(resId, null);
    }


    /**
    * 方法说明:  获取字符串
    * created by liuxiong on 2020/1/13 15:02
    */
    public static String getString(int resId) {
        return getResources().getString(resId);
    }
    /**
     * 方法说明:  获取有占位符的字符串
     * created by liuxiong on 2020/1/13 15:02
     */
    public static String getString(int resId, Object... objects) {
        return getResources().getString(resId,objects);
    }

    /**
     * 方法说明:  获取字符串数组
     * created by liuxiong on 2020/1/13 14:58
     */
    public static String[] getStringArray(int resId) {
        return getResources().getStringArray(resId);
    }

    /**
    * 方法说明:  获取资源id数组
    * created by liuxiong on 2020/1/13 14:58
    */
    @SuppressLint("Recycle")
    public static TypedArray getTypeArray(int resId) {
        return getResources().obtainTypedArray(resId);
    }

    /**
     * 方法说明:  获取dimens的数值并将单位转换成 px
     * created by liuxiong on 2020/1/13 14:58
     */
    public static int getDimens(int resId) {
        return getResources().getDimensionPixelSize(resId);
    }


}
